
#include <tenacitas_text_log_file/logger.h> 

using namespace tenacitas::text_log_file;



// #############################################################################
// change_file_policy

// -----------------------------------------------------------------------------
// std::string change_file_policy::m_dir_log( "./" );
// std::string change_file_policy::m_pgm_name ( "NO-PGM" );
// int64_t change_file_policy::m_pid ( -1 );
// uint64_t change_file_policy::m_max_file_size( 1024 * 1014 * 5 );

static std::string g_dir_log( "./" );
static std::string g_pgm_name ( "NO-PGM" );
static int64_t g_pid ( -1 );
static uint64_t g_max_file_size( 1024 * 1014 * 5 );


// -----------------------------------------------------------------------------
change_file_policy::ptr
change_file_policy::
create( boost::thread::id p_thread_id ) {
    if ( g_pid == -1 ) {
        throw make_log_exception ("Process ID not configured for "
                                  "change_file_policy ");
    }
    return ptr( new change_file_policy( p_thread_id ) );
}


// -----------------------------------------------------------------------------
change_file_policy::
change_file_policy( boost::thread::id p_thread_id )
    : m_thread_id( p_thread_id ),
      m_file_index( -1 ),
      m_current_file_name( ),
      m_dir_log( g_dir_log ),
      m_pgm_name( g_pgm_name ),
      m_pid( g_pid ),
      m_max_file_size( g_max_file_size ){}


// -----------------------------------------------------------------------------
void
change_file_policy::
config( const std::string & p_dir_log,
        const std::string & p_pgm_name,
        int64_t p_pid,
        uint64_t p_max_file_size ) {
    g_dir_log = p_dir_log;
    g_pgm_name = p_pgm_name;
    g_pid = p_pid;
    g_max_file_size = p_max_file_size;
}

// -----------------------------------------------------------------------------
void
change_file_policy::
on_thread_end( ofstream & p_file ) {

    if ( p_file.is_open() ) {
        p_file.close( );
    }

    // const std::string l_log_file_name_ended ( m_current_file_name +
    //                                           std::string( ".END" ) );

    // std::rename( m_current_file_name.c_str( ),
    //              l_log_file_name_ended.c_str( ) );

    // std::cout << "change_file_policy::on_thread_end: ";
    // std::cout << "m_file_index = " << m_file_index;
    // std::cout << ", m_dir_log = " << m_dir_log;
    // std::cout << ", m_pgm_name = " << m_pgm_name;
    // std::cout << ", m_pid = " << m_pid;
    // std::cout << ", m_max_file_size = " << m_max_file_size;
    // std::cout << std::endl;


    for ( int32_t l_i = 0; l_i <= m_file_index; ++l_i ) {
        std::cout << file_name( l_i ) << std::endl;
    }
}


// -----------------------------------------------------------------------------
bool
change_file_policy::
change_now( uint64_t p_file_size ) const {
    static const double ninety_per_cent_max =
        0.9 * static_cast<double>( m_max_file_size );

    const double l_tmp = static_cast<double>( p_file_size );

    bool l_change = ( l_tmp > ninety_per_cent_max );
                
    return l_change;
}

// -----------------------------------------------------------------------------
const std::string & 
change_file_policy::
file_name( int32_t p_file_index ) {
    std::stringstream l_stream;

    l_stream << m_dir_log << "/" << m_pgm_name << "_" << m_pid << "_";

    if ( m_thread_id == boost::thread::id( ) ) {
        l_stream << "0";
    }
    else {

        int64_t l_thread_id = tenacitas::log::th2int( m_thread_id );

        l_stream << std::dec << l_thread_id;
    }

    l_stream << "_" << std::setfill( '0' ) << std::setw( 10 ) << p_file_index;

    l_stream << ".log";

    m_current_file_name = l_stream.str( );

    return m_current_file_name;
}


// #############################################################################
// config_logger

// -----------------------------------------------------------------------------
void tenacitas::text_log_file::config_logger( const std::string & p_dir_log,
                                              const std::string & p_pgm_name,
                                              std::int64_t p_pid,
                                              tenacitas::log::level p_level,
                                              uint64_t p_max_file_size ) {

    /** extracting pgm name out of full pgm name path */
    std::string l_full_pgm_name( p_pgm_name );

    std::string::size_type l_ind = l_full_pgm_name.rfind( '/' );
    if ( l_ind == std::string::npos ) {
        l_ind = l_full_pgm_name.rfind( '\\' );
    }

    std::string l_pgm_name;

    if ( l_ind == std::string::npos ) {
        l_pgm_name = l_full_pgm_name;
    }
    else {
        l_pgm_name = l_full_pgm_name.substr( l_ind + 1 );
    }

    /** configuring factories */
    change_file_policy::config( p_dir_log, l_pgm_name,
                                p_pid, p_max_file_size );

    logger::set_level_all( p_level );
}




// #include <tenacitas_text_log_file/logger.h> 

// using namespace tenacitas::text_log_file;



// // #############################################################################
// // change_file_policy

// // -----------------------------------------------------------------------------
// std::string change_file_policy::m_dir_log( "./" );
// std::string change_file_policy::m_pgm_name ( "NO-PGM" );
// int64_t change_file_policy::m_pid ( -1 );
// uint64_t change_file_policy::m_max_file_size( 1024 * 1014 * 5 );
// //boost::thread_group change_file_policy::m_mergers;

// // -----------------------------------------------------------------------------
// void
// change_file_policy::
// config( const std::string & p_dir_log,
//         const std::string & p_pgm_name,
//         int64_t p_pid,
//         uint64_t p_max_file_size ) {
//     m_dir_log = p_dir_log;
//     m_pgm_name = p_pgm_name;
//     m_pid = p_pid;
//     m_max_file_size = p_max_file_size;
// }

// // -----------------------------------------------------------------------------
// void
// change_file_policy::
// on_thread_end( ofstream & p_file ) {

    
//     std::cout << "change_file_policy::on_thread_end" << std::endl;

//     if ( p_file.is_open() ) {
//         p_file.close( );
//     }

//     // const std::string l_log_file_name_ended ( m_current_file_name +
//     //                                           std::string( ".END" ) );

//     // std::rename( m_current_file_name.c_str( ),
//     //              l_log_file_name_ended.c_str( ) );
//     merge( m_dir_log, m_pgm_name, m_pid, m_thread_id, m_file_index );
// }

// // -----------------------------------------------------------------------------
// void
// change_file_policy::
// merge( const std::string & p_dir_log,
//        const std::string & p_pgm_name,
//        int64_t p_pid,
//        boost::thread::id p_thread_id,
//        int32_t p_file_index ) {

//     std::stringstream l_stream;

//     l_stream <<  "/var/tmp/" << tenacitas::log::th2int( p_thread_id ) << ".log";

//     std::ofstream l_out ( l_stream.str( ).c_str( ) );
    
    
//     for ( int32_t l_i = 0; l_i <= p_file_index; ++l_i ) {
//         const std::string l_file_name = file_name( p_dir_log,
//                                                    p_pgm_name,
//                                                    p_pid,
//                                                    p_thread_id,
//                                                    l_i );
//         l_out << l_file_name << std::endl;
//     }
// }


// // -----------------------------------------------------------------------------
// bool
// change_file_policy::
// change_now( uint64_t p_file_size ) const {
//     static const double ninety_per_cent_max =
//         0.9 * static_cast<double>( m_max_file_size );

//     const double l_tmp = static_cast<double>( p_file_size );

//     bool l_change = ( l_tmp > ninety_per_cent_max );
                
//     return l_change;
// }

// // -----------------------------------------------------------------------------
// std::string
// change_file_policy::
// file_name( const std::string & p_dir_log,
//            const std::string & p_pgm_name,
//            int64_t p_pid,
//            boost::thread::id p_thread_id,
//            int32_t p_file_index ) {

//     std::stringstream l_stream;

//     l_stream << p_dir_log << "/" << p_pgm_name << "_" << p_pid << "_";

//     if ( p_thread_id == boost::thread::id( ) ) {
//         l_stream << "0";
//     }
//     else {

//         int64_t l_thread_id = tenacitas::log::th2int( p_thread_id );

//         l_stream << std::dec << l_thread_id;
//     }

//     l_stream << "_" << std::setfill( '0' ) << std::setw( 10 ) << p_file_index;

//     l_stream << ".log";

//     return l_stream.str( );
// }
    


// // #############################################################################
// // config_logger

// // -----------------------------------------------------------------------------
// void tenacitas::text_log_file::config_logger( const std::string & p_dir_log,
//                                               const std::string & p_pgm_name,
//                                               std::int64_t p_pid,
//                                               tenacitas::log::level p_level,
//                                               uint64_t p_max_file_size ) {

//     /** extracting pgm name out of full pgm name path */
//     std::string l_full_pgm_name( p_pgm_name );

//     std::string::size_type l_ind = l_full_pgm_name.rfind( '/' );
//     if ( l_ind == std::string::npos ) {
//         l_ind = l_full_pgm_name.rfind( '\\' );
//     }

//     std::string l_pgm_name;

//     if ( l_ind == std::string::npos ) {
//         l_pgm_name = l_full_pgm_name;
//     }
//     else {
//         l_pgm_name = l_full_pgm_name.substr( l_ind + 1 );
//     }

//     /** configuring factories */
//     change_file_policy::config( p_dir_log, l_pgm_name,
//                                 p_pid, p_max_file_size );

//     logger::set_level_all( p_level );
// }
